<?php
/**
 * Created by PhpStorm.
 * User: 李贾
 * Date: 2019/11/7
 * Time: 10:59
 */

namespace Alisms;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Alisms
{

    /**
     * @param array $config 配置信息
     * @param string $tempCode 模板id
     * @param string $phone 电话号码
     * @param string $code 验证码
     * @return array
     */
    public function send($config,$phone,$code){
        AlibabaCloud::accessKeyClient($config['accessKeyId'], $config['accessKeySecret'])
            ->regionId('cn-hangzhou')
            ->asGlobalClient();
        try {
            $result = AlibabaCloud::rpcRequest()
                ->product('Dysmsapi')
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->options([
                    'query' => [
                        'PhoneNumbers' => $phone,
                        'SignName' => $config['signName'],
                        'TemplateCode' => $config['TemplateCode'],
                        'TemplateParam' => json_encode($code)
                    ],
                ])
                ->request();
            $res =  $result->toArray();
            if ($res['Message'] == 'OK' && $res['Code']== 'OK'){
                return true;
            }else{
                return $res;
            }
        } catch (ClientException $e) {
            return false;
        } catch (ServerException $e) {
            return false;
        }
    }
}